package net.lovesrc.allin.jobs.backup;

import com.alibaba.fastjson.JSONObject;
import net.lovesrc.allin.biz.lbc.BaseLbcService;
import net.lovesrc.allin.biz.lbc.LbcRegisterService;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;

import java.util.Date;

//@Component
@Profile("geliclive")
public class LbcAuthIdJob extends LbcBaseJob {

    @Autowired
    private LbcRegisterService lbcRegisterService;

    @Override
    public void runOnce() throws Exception {
        {
            JSONObject row = queryForRow(jdbcTemplate,
                    "select mobile from t_lbc_account where idStatus is null and insertTime < ? order by rand() limit 1",
                    DateUtils.addMinutes(new Date(), -5)
            );
            if (row != null) {
                lbcRegisterService.authId(row.getString("mobile"), BaseLbcService.Proxy.PROXY_GELIC);

            }
        }
        {
            JSONObject row = queryForRow(jdbcTemplate,
                    "select mobile from t_lbc_account where idStatus = 'submitted' and idResStatus = 'success' and idTime < ? order by rand() limit 1",
                    DateUtils.addSeconds(new Date(), -10)
            );
            if (row != null) {
                String mobile = row.getString("mobile");
                lbcRegisterService.syncStatus(mobile, BaseLbcService.Proxy.PROXY_GELIC);
            }
        }
    }

    @Override
    protected long intervalMilliseconds() {
        return 1000 * RandomUtils.nextInt(60, 100);
    }

}
